﻿using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Resources;

using CE.User;
using CE.Global;
using CE.Database;
using CE.InputOut;
using CE.CarInsurance;
using CE.Mail;

public partial class carinsurance_renewal_renewal_notice : System.Web.UI.Page
{
    private DateTime dateTimeNow = CE.Global.General.GetUTCDateTime();

    protected override void InitializeCulture()
    {
        String selectedCulture = CE.Global.General.SetCulture();
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/SiteUserSecureLogin.aspx");
        }

        if (Session[General.UserRole] != null)
        {
            if (Convert.ToInt32(Session[General.UserRole]) <= 100)
                Response.Redirect("~/carinsurance/PendingApplication.aspx");
        }

        //if (Session[General.IsRenewalAgent] != null && Session[General.IsRenewalAgent].ToString() == "0")
        //{
        //    Response.Redirect("~/SiteUserHome.aspx");
        //}

        if (!IsPostBack)
        {
            lblError.Text = "";
            lblContent.Text = "";

            DateTime lastday = dateTimeNow.AddDays(2);
            DateTime beforeExpDate = dateTimeNow.AddMonths(-1);

            for (int _m = 0; _m <= 5; _m++)
            {
                ddlMonth1.Items.Insert(_m, new ListItem(beforeExpDate.AddMonths(_m).ToString("MMM yyyy"), beforeExpDate.AddMonths(_m).ToString("yyyyMM")));
            }

            ddlMonth1.Text = dateTimeNow.ToString("yyyyMM");
        }
        else
        {
            lblError.Text = "";
            lblSuccessLead.Text = "";
            lblErrorLead.Text = "";
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblError.Text = "";
        txtAgentId.Text = Session[CE.Global.General.UserId].ToString();
        //grvApplication.SelectedIndex = -1;
        //grvApplication_SelectedIndexChanged(sender, e);
        divSearch.Visible = true;
        LoadGrid();
    }

    //OnSelectedIndexChanged="grvApplication_SelectedIndexChanged" 
    //protected void grvApplication_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    if (grvApplication.SelectedIndex >= 0)
    //    {
    //        GridViewRow _gvr = grvApplication.SelectedRow;
    //        try
    //        {
    //            string LastYearRefNo = _gvr.Cells[0].Text;
    //            Int64 _agentLeadId = 0;
    //            Int64 _customerId = 0;
    //            if (!Int64.TryParse(_gvr.Cells[5].Text, out _customerId))
    //            {
    //                _customerId = 0;
    //            }
    //            Int64 _carId = 0;
    //            Int64 _outCarID = 0;
    //            if (!Int64.TryParse(_gvr.Cells[6].Text, out _carId))
    //            {
    //                _carId = 0;
    //            }
    //            Int64 _agentID = 0;
    //            if (!Int64.TryParse(txtAgentId.Text, out _agentID))
    //            {
    //                _agentID = 0;
    //            }
    //            if (_agentLeadId > 0 && _outCarID > 0)
    //            {
    //                string _strParameters = "leadid=" + _agentLeadId.ToString() + "&calltype=100";

    //                _strParameters += "&renew=2";
    //                _strParameters += "&custid=" + _customerId.ToString() + "&carid=" + _outCarID.ToString();
    //                _strParameters += "&refno=" + LastYearRefNo;

    //                if (_customerId > 0 && _carId > 0)
    //                {
    //                    Response.Redirect("~/carinsurance/offline/ViewCINoMatchFound.aspx?" + _strParameters.ToString());
    //                }
    //                else
    //                {
    //                    lblError.Text = "Please try again later.";
    //                }
    //            }
    //        }
    //        catch (Exception Ex)
    //        {
    //            lblError.Text = Ex.ToString();
    //        }
    //    }
    //}

    protected void grvApplication_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string custId = DataBinder.Eval(e.Row.DataItem, "RefCEUserId").ToString();
            string carId = DataBinder.Eval(e.Row.DataItem, "CECarId").ToString();
            string renewalId = DataBinder.Eval(e.Row.DataItem, "RenewalId").ToString();
            string refno = DataBinder.Eval(e.Row.DataItem, "QuoteRefNo").ToString();
            Literal ltr = (Literal)e.Row.FindControl("ltrLink");
            ltr.Text = "<br><a class='popup-notice' href='renewal/renewal-notice-update.aspx?uid=" + custId + "&cid=" + carId + "&qno=" + refno + "'>update</a><br>";

            Literal ltrRed = (Literal)e.Row.FindControl("ltrRed");
            if (renewalId.Trim() != "")
            {
                ltrRed.Text = "<img alt=\"Renewal Updated\" src=\"../images/red-dot.png\" />";
            }


            //string sharedlink = "http://zebra/cx/";
            //string cName = e.Row.Cells[1].Text;
            //string cPlate = e.Row.Cells[2].Text;
            //string cDate = e.Row.Cells[3].Text;
            //if (cDate.Trim() != "")
            //{
            //    HyperLink linkPdf = (HyperLink)e.Row.FindControl("linkPdf");
            //    string[] path = cDate.Split('/');
            //    linkPdf.Text = "Pdf";
            //    if (cPlate.Trim() != "ป้ายแดง")
            //    {
            //        linkPdf.NavigateUrl = sharedlink + (int.Parse(path[2]) + 1) + "/" + path[1] + "/" + cPlate.Trim() + ".pdf";
            //    }
            //    else
            //    {
            //        linkPdf.NavigateUrl = sharedlink + (int.Parse(path[2]) + 1) + "/" + path[1] + "/" + cName.Trim() + ".pdf";
            //    }
            //}
        }
    }

    public void LoadGrid()
    {
        lblError.Text = "";
        DataTable _dtSummary = new DataTable();
        string _SearchKey = Server.HtmlEncode(txtSearch.Text);
        Int16 _Language = Convert.ToInt16(lblLangOption.Text);

        MyDatabase _db = new MyDatabase("GG_CRMDBConnectionString");
        string _spName = "[sprocCXCRMNonGGRenewalNoticeSelectedList]";
        SortedList _sPara = new SortedList();

        _sPara.Add("@SearchKey", _SearchKey);
        _sPara.Add("@Mobile", txtMobileNoSearch.Text);
        _sPara.Add("@ReportDate1", ddlMonth1.Text);

        _dtSummary = _db.GetDataTableBySP(_spName, _sPara);
        if (_dtSummary != null && _dtSummary.Rows.Count > 0)
        {
            lblError.Text = "";
            object rTotal = _dtSummary.Compute("SUM(RenewPremium)", "");
            if(rTotal != null)
                lblRenewTotal.Text = decimal.Parse(rTotal.ToString()).ToString("#,##0.00");

            grvApplication.DataSource = _dtSummary;
            grvApplication.DataBind();
        }
        else
        {
            lblRenewTotal.Text = "";

            grvApplication.DataSource = null;
            grvApplication.DataBind();
        }
    }

    protected void btnLeads_Click(object sender, EventArgs e)
    {
        MyDatabase _db = new MyDatabase();
        string _spName = "[sprocCXCRMNonGGRenewalLeadAssign]";
        SortedList _sPara = new SortedList();
        _sPara.Add("@AgentId", Session[CE.Global.General.UserId]);
        int rtn = Convert.ToInt32(_db.ExecuteSP(_spName, _sPara));
        if (rtn > 0)
        {
            lblSuccessLead.Text = "Saved Successfully";
        }
        else
        {
            lblErrorLead.Text = "Error on " + Resources.General.GenerateLead;
        }
    }
}
